<?php

declare (strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Model;

use Hyperf\Database\Model\SoftDeletes;
class Collect extends Model
{
    use SoftDeletes;
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected ?string $table = 'collect';
    /**
     * 用户
     * @return \Hyperf\Database\Model\Relations\BelongsTo
     */
    public function user()
    {
        return $this->belongsTo(user::class, 'user_id');
    }
    /**
     * 信息
     * @return \Hyperf\Database\Model\Relations\BelongsTo
     */
    public function information()
    {
        return $this->belongsTo(Information::class, 'information_id');
    }
    /**
     * 接口返回
     * @return array
     */
    public function toApi()
    {
        /** @var Information $information */
        $information = $this->information;
        $infoObj = $information->getInfoObject();
        $data = ['id' => $this->id, 'information_id' => $information->id ?? '', 'img' => stripslashes(($information->imgs()->first()->url ?? '')), 'type' => $this->information->type ?? '', 'type_text' => $this->information->type_text ?? '', 'title' => $infoObj->title ?? $infoObj->content ?? '', 'user' => $information->user->nickname ?? '', 'header' => $information->user->header->url ?? '', 'create_time' => $this->create_time->toDateTimeString()];
        if (in_array($information->type, [4, 5]) && $information->all_type) {
            $data['type_text'] .= '-' . $information->all_type_text;
        }
        return $data;
    }
}